local skynet = require "skynet"
local SlotTool = require "slottool"
local tabletool = require "tabletool"
local filelog = require "filelog"
local base = require "base"
local redisdao = require "redisdao"
local configdao = require "configdao"
local json = require "cjson"
json.encode_sparse_array(true,1,1)

local CMD = {}


local leinumbers_weights_ten = {
	[1] = 120,
	[2] = 80,
	[3] = 40,
	[4] = 15,
	[5] = 10,
    [6] = 5,
}

local leinumbers_weights_seven = {
    [1] = 90,
    [2] = 50,
    [3] = 30,
    [4] = 8,
    [5] = 5,
}

local leinumbers_weights_five = {
    [1] = 70,
    [2] = 60,
    [3] = 5,
}

local lizhuo_weights = {
    [1] = 0,
    [2] = 0,
    [3] = 60,
    [4] = 80,
    [5] = 100,
}

local robot_leave_table_weight = {
    [1] = {
        [1] = 100,
        [2] = 0,
    },
    [2] = {
        [1] = 90,
        [2] = 10,
    },
    [3] = {
        [1] = 80,
        [2] = 20,
    },
    [4] = {
        [1] = 70,
        [2] = 30,
    },
    [5] = {
        [1] = 60,
        [2] = 40,
    },
    [6] = {
        [1] = 0,
        [2] = 100,
    },
}

local robot_qhb_weight = {
    { per = 0, qhb_num = 0},
    { per = 90, qhb_num = 1},
    { per = 10, qhb_num = 2}
}


local delay_time_weights = {
     [1] = 20,
     [2] = 30,
     [3] = 40,
     [4] = 50,
     [5] = 60,
     [6] = 120,
}


local bury_money_weights = {
    [1] = 60,
    [2] = 20,
    [3] = 10,
    [4] = 5,
    [5] = 5,
}

local function getqhb_weightconf()
    local hbslnodeconf = nil
    local qhb_weightconf = nil
    local result, data = redisdao.query_data(".gmmqredis", "get", "hbslnode_conf")
    if result and data then
        hbslnodeconf = json.decode(data)
        qhb_weightconf = tabletool.deepcopy(hbslnodeconf.robot_qhb_weight)
    end
    if not qhb_weightconf then
        return nil
    else
        return qhb_weightconf
    end
end

local function get_tax_rate_from_redis(gamename)
    local tax_rate = 0.01   --默认税率0.01
    local key = (gamename or "") .. "_rate"
    local result, result_data = redisdao.query_data(".gmmqredis", "get", key)
    if result and result_data then
        tax_rate = tonumber(result_data) or 0.01
        -- if tax_rate > 0.5 or tax_rate < 0.01 then
        --     tax_rate = 0.01
        -- end
    end
    return tax_rate
end

function CMD.get_service_fee_rate(gamename)
    return SlotTool.get_service_fee_rate_from_redis(gamename)
end

function CMD.update_system_inventory(gamename)
    return SlotTool.get_inventory_from_redis(gamename)
end


function CMD.update_system_water_profit(gamename, update_value)
    local key = (gamename or "") .. "_system_water_profit"
    local _,sys_water_profit = redisdao.query_data(".gmmqredis", "get", key)
    if not sys_water_profit then
        sys_water_profit = 0
    else
        sys_water_profit = tonumber(sys_water_profit) or 0
    end
    local beginvalue = sys_water_profit
    local endvalue = sys_water_profit + update_value
    if beginvalue ~= endvalue then
        redisdao.query_data(".gmmqredis", "set", key, endvalue)
    end
end

function CMD.handle_balance_result(gamename, total_change, total_water, total_bet, comment, sys_change)
    local tax_rate = get_tax_rate_from_redis(gamename)
    local inventory_val = -total_change
    local profit_val = math.floor(tax_rate * inventory_val)
    filelog.sys_error("----------handle_balance_result------", profit_val)
    local result1 = SlotTool.update_system_profit(gamename, profit_val, comment)
    SlotTool.update_system_inventory(gamename, inventory_val, comment)
    -- CMD.update_system_water_profit(gamename, sys_change - total_change)
    -- filelog.sys_error("-----handle_balance_result----", gamename, total_change, total_water, total_bet)
    -- local tax_rate = SlotTool.get_tax_rate_from_redis(gamename)
    -- local profit_val = math.floor(total_bet * tax_rate * 100) / 100
    -- --local profit_val = math.floor(total_bet * tax_rate)
    -- local inventory_val = -total_change
    -- CMD.update_system_water_profit(gamename, sys_change - total_change)
    -- filelog.sys_error("-------tax_rate----", tax_rate, profit_val, inventory_val)
    -- --更新收益值
    -- local result1 = SlotTool.update_system_profit(gamename, profit_val, comment)
    -- if result1 then
    --     inventory_val = inventory_val - profit_val
    -- end

    -- --更新库存值，扣除抽水
    -- local service_fee_rate = SlotTool.get_service_fee_rate_from_redis(gamename)
    -- local service_fee_val = math.floor(total_water * service_fee_rate * 100) / 100
    -- --local service_fee_val = math.floor(total_water * service_fee_rate)
    -- inventory_val = inventory_val - service_fee_val
    -- filelog.sys_error("------service_fee_rate------", service_fee_rate, service_fee_val, inventory_val)
    -- return SlotTool.update_system_inventory(gamename, inventory_val, comment)
end


function CMD.get_can_leveltable(game_times)
    local game_times = game_times or 1
    local is_leave = true
    if robot_leave_table_weight[game_times] then
        local base_temp = base.get_random(1,100)
        for index, value in ipairs(robot_leave_table_weight[game_times]) do
            base_temp = base_temp - value
            if base_temp <= 0 then
                if index == 1 then is_leave = false end
                break
            end
        end
    end
    if game_times == 0 then
        is_leave = false
    end
    return is_leave
end

---根据权重获取中雷红包数量
function CMD.get_leinum_nums(hb_all_nums)
	local leinum_nums = 0
	local total_w = 0
    local w_times = 0
    if hb_all_nums == 10 then
        total_w = SlotTool.calculate_total_weight(leinumbers_weights_ten)
        w_times = SlotTool.get_random_result_by_weightlist(leinumbers_weights_ten, total_w)
        leinum_nums = w_times - 1
        if leinum_nums < 0 then
    	   leinum_nums = 0
        end
    elseif hb_all_nums == 7 then
        total_w = SlotTool.calculate_total_weight(leinumbers_weights_seven)
        w_times = SlotTool.get_random_result_by_weightlist(leinumbers_weights_seven, total_w)
        leinum_nums = w_times - 1
        if leinum_nums < 0 then
           leinum_nums = 0
        end
    elseif hb_all_nums == 5 then
        total_w = SlotTool.calculate_total_weight(leinumbers_weights_five)
        w_times = SlotTool.get_random_result_by_weightlist(leinumbers_weights_five, total_w)
        leinum_nums = w_times - 1
        if leinum_nums < 0 then
           leinum_nums = 0
        end
    end
    return leinum_nums
end


function CMD.get_bury_money()
    local bury_money = 0
    local total_w = 0
    total_w = SlotTool.calculate_total_weight(bury_money_weights)
    bury_money = SlotTool.get_random_result_by_weightlist(bury_money_weights, total_w)
    if bury_money > 0 then
        bury_money = bury_money * 10 * 100
    end
    return bury_money
end

function CMD.get_q_allhb()
    local get_hb_num = 0
    local robot_qhb_weight_temp = getqhb_weightconf()
    if robot_qhb_weight_temp ==  nil then
        robot_qhb_weight_temp = robot_qhb_weight
    end
    local temp = base.get_random(0,100)
    ---filelog.sys_error("-----------robot_qhb_weight_temp------", robot_qhb_weight_temp)
    for hb_num, value in ipairs(robot_qhb_weight_temp) do
        temp = temp - value.per
        if temp <= 0 then
            get_hb_num = value.qhb_num
            break
        end
    end
    return get_hb_num
end

function CMD.get_delaytime()
    local delay_time = 0
    local total_dealy_weights = SlotTool.calculate_total_weight(delay_time_weights)
    local delay_time = SlotTool.get_random_result_by_weightlist(delay_time_weights, total_dealy_weights)
    if delay_time == 0 then
        delay_time = 1
    end
    return delay_time
end


function CMD.init()
    local tax_rate = 1.0   --默认税率0.3
    local tablelist_conf = tabletool.deepcopy(configdao.get_common_conf("tablelist_conf"))
    ---filelog.sys_error("---------tablelist_conf---------------", tablelist_conf)
    for key, matchitem in pairs(tablelist_conf) do
        local key = "hbsl_diamond_" .. tostring(key) .. "_rate"
        local result, result_data = redisdao.query_data(".gmmqredis", "get", key)
        if result and not result_data then
            redisdao.query_data(".gmmqredis", "set", key, tax_rate)
        end
    end
end

skynet.start(function() 
	skynet.dispatch("lua",function(session, source, cmd, ...)
        local f = CMD[cmd]
        if f then
            skynet.retpack(f(...))
        else
            skynet.retpack()
        end
    end)
    CMD.init()
end)